package cn.hl.hm.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.hl.hm.dao.DriverMapper;
import cn.hl.hm.dao.VehicleLengthMapper;
import cn.hl.hm.dao.VtVlMapper;
import cn.hl.hm.entity.Driver;
import cn.hl.hm.entity.Route;
import cn.hl.hm.entity.RouteVehiclePrice;
import cn.hl.hm.entity.VehicleLength;
import cn.hl.hm.entity.VehicleType;
//import cn.hl.hm.entity.VtVl;
import cn.hl.hm.service.RouteService;
import cn.hl.hm.service.RouteVehiclePriceService;
import cn.hl.hm.service.VehicleTypeService;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.Result;

/**
 * 车型长度
 * 
 * @author LBJ
 *
 */
@Controller
@RequestMapping("/vehicleLengthController")
public class VehicleLengthController {
	private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

	@Autowired
	VehicleLengthMapper vehicleLengthMapper;
	@Autowired
	DriverMapper driverMapper;
	@Autowired
	VtVlMapper vtVlMapper;
	@Autowired
	VehicleTypeService  vehicleTypeService;
	@Autowired
	RouteService routeService;
	
	@Autowired
	RouteVehiclePriceService rvpService;
	/**
	 * 当前进行中的订单 1：当前订单列表 2：订单收入列表，3历史订单列表 4 所有订单列表
	 * 
	 * @param driverId
	 * @param pageId
	 * @param pageCount
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/selectVehicleLengthList", method = RequestMethod.POST)
	public String selectOrderList(

			HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.info("个人中心/车长管理---车长列表接口/车长列表接口---vehicleLengthController/selectVehicleLengthList");
		request.setCharacterEncoding("UTF-8");
		try {

			List<VehicleLength> vehicleTypeList = vehicleLengthMapper.selectVehicleLength();
			 
//			if(vehicleTypeList!=null&&vehicleTypeList.size()>0){
//				for (VehicleLength vehicleLength2 : vehicleTypeList) {
//					List<Driver> list = new ArrayList<>();
//					//判断有无司机绑定
//					VtVl v = new VtVl();
//					v.setVehicleLengthId(vehicleLength2.getId());
//					List<VtVl> selectVtVlList = vtVlMapper.selectVtVlList(v);
//					if(selectVtVlList!=null&&selectVtVlList.size()>0){
//					
//						
//						for (VtVl vtVl : selectVtVlList) {
//							Integer id = vtVl.getId();
//							Driver driver = new Driver();
//							driver.setVtVlId(id.toString());
//							List<Driver> selectDriver = driverMapper.selectDriver(driver);
//							if(selectDriver!=null&&selectDriver.size()>0){
//								for (Driver driver2 : selectDriver) {
//									list.add(driver2);
//								}
//							}
//							
//						}
//					}
//					vehicleLength2.setDriverlist(list);
//				}
//			}
							
			Result.ok(vehicleTypeList, InfoCode.SUCCESS, response);
		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
		}
		return null;
	}

	/**
	 * @Title: send @Description:添加车型 @date: 2017年1月5日 @author: 星矢 @throws  --新
	 */
	@RequestMapping(value = "/addVehicleLength", method = RequestMethod.POST)
	public String addVehicleType(VehicleLength vehicleLength, HttpServletRequest request,
			HttpServletResponse response) {
		logger.info("车长管理---添加车长---vehicleLengthController/addVehicleLength");
		try {
			vehicleLength.setCreateTime(new Date().toLocaleString());

			List l = vehicleLengthMapper.selectVLByLength(vehicleLength.getVehicleLength());
			if(l!=null && l.size()>0){
				Result.ok(null, InfoCode.VEHICLE_LENGTH_ERROR, response);
				return null;
			}else{
			int result = vehicleLengthMapper.insertSelective(vehicleLength); // 添加车长
			if (result > 0) {
			 
//				VehicleType param = new VehicleType();
//				param.setStatus("0");
//				Route r = new Route();
//				r.setRouteStatus("0");
//				List<Route> route = routeService.selectRoute(r);
//				
//				List<VehicleType> vehicleTypeList = vehicleTypeService.selectVehicleType(param);
//				for (VehicleType vt : vehicleTypeList) {// 添加车型后，需要关联所有的车长
////					 VtVl vtvl = new VtVl();
////					 vtvl.setVehicleLengthId(vehicleLength.getId());
////					 vtvl.setVehicleTypeId(vt.getId());
////					 vtvl.setCreateTime(new Date().toLocaleString());
////					 vtVlMapper.insertSelective(vtvl);
//					// 增加一个车型，则所有路线都增加该车型double
//						for (Route route2 : route) {  //添加车型后，需要关联所有的路线
//							RouteVehiclePrice rvp = new RouteVehiclePrice();
//							rvp.setPrice(new BigDecimal(0));
//							rvp.setRouteId(route2.getId());
//							rvp.setDateTime(new Date().toLocaleString());
//							rvp.setVtVlId(vtvl.getId());
//							rvpService.addRouteVehiclePrice(rvp);
//						}
//				}
				Result.ok(null, InfoCode.SUCCESS, response);
				return null;
			} else {
				Result.ok(null, InfoCode.FAILURE, response);
				return null;
			}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}

	}
//	/**
//	 * @Title: send @Description:添加车型 @date: 2017年1月5日 @author: 星矢 @throws  --旧
//	 */
//	@RequestMapping(value = "/addVehicleLength", method = RequestMethod.POST)
//	public String addVehicleType(VehicleLength vehicleLength, HttpServletRequest request,
//			HttpServletResponse response) {
//
//		try {
//			vehicleLength.setCreateTime(new Date().toLocaleString());
//
//			List l = vehicleLengthMapper.selectVLByLength(vehicleLength.getVehicleLength());
//			if(l!=null && l.size()>0){
//				Result.ok(null, InfoCode.VEHICLE_LENGTH_ERROR, response);
//				return null;
//			}else{
//			int result = vehicleLengthMapper.insertSelective(vehicleLength); // 添加车长
//			if (result > 0) {
//				Result.ok(null, InfoCode.SUCCESS, response);
//				return null;
//			} else {
//				Result.ok(null, InfoCode.FAILURE, response);
//				return null;
//			}
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//			Result.ok(null, InfoCode.FAILURE, response);
//			return null;
//		}
//
//	}

	/**
	 * @Title: send @Description:添加车型 @date: 2017年1月5日 @author: 星矢 @throws
	 */
	@RequestMapping(value = "/deleteVehicleLength", method = RequestMethod.POST)
	public String deleteVehicleLength(VehicleLength vehicleLength, HttpServletRequest request,
			HttpServletResponse response) {

		try {
			vehicleLength.setStatus(1);
			//int result = vehicleLengthMapper.deleteByPrimaryKey(vehicleLength.getId()); //删除车长
			int  result = vehicleLengthMapper.updateByPrimaryKeySelective(vehicleLength);
			if (result > 0) {
				Result.ok(null, InfoCode.SUCCESS, response);
				return null;
			} else {
				Result.ok(null, InfoCode.FAILURE, response);
				return null;

			}
		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}

	}

}
